//
//  HLTBandCardTableViewCell.m
//  huiliantong
//
//  Created by hlt on 15/8/31.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTBandCardTableViewCell.h"
#import "HLTyijianbangkaTableViewController.h"
#import "HLTzidingyibangkaTableViewController.h"
@implementation HLTBandCardTableViewCell

- (void)awakeFromNib {
    // Initialization code
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    self.username.text=acc.custPhone;
    if ([acc.cardNo isEqual:@""]) {
        self.cardno.text=@"未绑定汇联通卡 !";
        self.yue.text=@"未绑定无法完成商城支付";
        
    }
    else
    {
        self.cardno.text=[NSString stringWithFormat:@"卡号:%@",acc.cardNo];
        self.yue.text=[NSString stringWithFormat:@"余额:%@",acc.YKTCardBalance];
         [self.zidingyikaika setTitle:@"申请补卡" forState:UIControlStateNormal];//设置button的title
        
          [self.zidongkaika setTitle:@"一键解绑汇联通卡" forState:UIControlStateNormal];//设置button的title
        
    }
    
}



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件 这个是把这个xib文件全部加载了，然后取第一个
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        
        // 如果路径不存在，return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类，return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
    
    
}


- (IBAction)yijiankaika:(id)sender {
    
    
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    if ([acc.cardNo isEqual:@""])
    {
        //这个是一键开卡的事件
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message:@"你好，如果您已领取了工会服务卡，可以不需要本次操作，该操作汇新开户一张普通卡，确定要办理一张普通卡吗？"
                                                     delegate:self
                                            cancelButtonTitle:@"取消"
                                            otherButtonTitles:@"好",nil];
        [alert show];

        
    }
    else
    {
        
        
        UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
        view1.backgroundColor=[UIColor grayColor];
        view1.alpha=0.4;
        [self.parent.view addSubview:view1];
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        indicatorView.delegate = self;
        indicatorView.numberOfCircles = 3;
        indicatorView.radius = 20;
        indicatorView.internalSpacing = 3;
        indicatorView.center = self.parent.view.center;
        [indicatorView startAnimating];
        [self.parent.view addSubview:indicatorView];
        
        
        
        //这个是一键解绑的事件
        
        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        //一键解绑接口 (username=18285117540，num=18285117540，cardNum=卡号，queryType=‘S’)
        NSString *url=[yijianjiebang stringByAppendingFormat:@"&username=%@&num=%@&cardNum=%@&queryType=‘S’",acc.custPhone,acc.custPhone,acc.cardNo];
        
        [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
            
            NSLog(@"%@",ary);
            //将nsnumber 装换成nsstring
//            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
//            NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
            
            acc.cardNo=@"";
            [[AccountTool sharedAccountTool] addAccounts:acc];
            UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"解绑成功"
                                                          message:nil
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                                otherButtonTitles:@"好",nil];
            [alert show];
            
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            
            
        } faliure:^(NSString *error) {
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
        }];
        

        
        
        
        
        
    }
    
    
    
    
    
}





- (IBAction)zidingyikabangding:(id)sender {
    
    
    [EYInputPopupView popViewWithTitle:@"请输入卡号" contentText:@"卡号"
                                  type:EYInputPopupView_Type_single_line_text
                           cancelBlock:^{
                               
                           } confirmBlock:^(UIView *view, NSString *text) {
                               
                               
                               
                               UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                               HLTzidingyibangkaTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTzidingyibangkaTableViewController"];
                               vc.kahao=text;
                               [self.parent.navigationController pushViewController:vc animated:NO];
                               
                           } dismissBlock:^{
                               
                           }];
    
}


//uialert的委托方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==1) {
        //点击了好，进入下一个页面
        
        //这个是加载等待界面的方法
        UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
        view1.backgroundColor=[UIColor grayColor];
        view1.alpha=0.4;
        [self.parent.view addSubview:view1];
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        indicatorView.delegate = self;
        indicatorView.numberOfCircles = 3;
        indicatorView.radius = 20;
        indicatorView.internalSpacing = 3;
        indicatorView.center = self.parent.view.center;
        [indicatorView startAnimating];
        [self.parent.view addSubview:indicatorView];
        
        
        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        NSString *url=[yijianbangka stringByAppendingFormat:@"&user_name=%@",acc.custPhone];
        
        [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
            //停止等待
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            
            //将nsnumber 装换成nsstring
            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
            
            
            if ([str isEqual:@"2"]) {
                
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                HLTyijianbangkaTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTyijianbangkaTableViewController"];
                
                vc.kahao=ary[@"CardNum"];
                vc.pwd1=ary[@"password"];
                vc.liushuihao1=ary[@"transaction_id"];
                [self.parent.navigationController pushViewController:vc animated:NO];
            }
            else
            {
                UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"一键绑定卡失败"
                                                              message:ary[@"msg"]
                                                             delegate:nil
                                                    cancelButtonTitle:@"好"
                                                    otherButtonTitles:nil,nil];
                [alert show];
                
            }
            
            
            
        } faliure:^(NSString *error) {
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
        }];
        
        
        
        
        
        
       
    }
}


//这个是回调方法
#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
